/**
 * 
 */
package com.feib.stms.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Service;

import com.feib.stms.dao.DeliveryPaymentSumDao;
import com.feib.stms.dao.jdbc.SqlQueryMapProvider;
import com.feib.stms.model.DeliveryPayment;
import com.feib.stms.model.DeliveryPaymentSum;
import com.feib.stms.pagination.PaginatedList;
import com.feib.stms.service.DeliveryPaymentSumService;

/**
 * @author Jimmy Liu
 * 
 */
@Service("deliveryPaymentSumService")
public class DeliveryPaymentSumServiceImpl extends BaseServiceImpl implements
		DeliveryPaymentSumService {

	private static final long serialVersionUID = -4712283332423919232L;

	@Resource(name = "deliveryPaymentSumDao")
	private DeliveryPaymentSumDao deliveryPaymentSumDao;
	
	@Resource(name="dataSource")
	protected DataSource dataSource;

	@Resource(name="temporaryBaseDirectory")
	protected String temporaryBaseDirectory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.feib.stms.service.DeliveryPaymentChkSumService#deleteByTempUploadFilePk
	 * (java.lang.Long)
	 */
	public int deleteByTempUploadFilePk(Long tempUploadFilePk) {

		return deliveryPaymentSumDao
				.deleteByTempUploadFilePk(tempUploadFilePk);
	}

	public PaginatedList<DeliveryPaymentSum> findPaginatedListByPaymentFileContent(
			DeliveryPayment entity, Date deliveryDateFrom,
			Date deliveryDateTo, String amountStatus,
			List<String> constrainSecuritiesNos,
			PaginatedList<DeliveryPaymentSum> paginatedList) {
		
		return deliveryPaymentSumDao.findPaginatedListByPaymentFileContent(
				entity, deliveryDateFrom, deliveryDateTo, amountStatus,
				constrainSecuritiesNos, paginatedList);
	}

	public String exportExcelByPaymentFileContent(DeliveryPayment entity,
			Date deliveryDateFrom, Date deliveryDateTo, String amountStatus,
			List<String> constrainSecuritiesNos,
			PaginatedList<DeliveryPaymentSum> paginatedList) throws Exception 
	{
		SqlQueryMapProvider sqlQueryMapProvider =  deliveryPaymentSumDao.getSqlQueryMapProviderByPaymentFileContent(entity, deliveryDateFrom, deliveryDateTo, amountStatus, constrainSecuritiesNos, paginatedList);
		
		String sheetName = "交割款明細表";
		
		String[] headers = new String[]{"帳號", "客戶戶名", "交割金額", "不足金額"
				, "交割日期", "營業員", "狀態"};
		
		DeliveryPaymentChkSumExcelExportor exportor = new DeliveryPaymentChkSumExcelExportor(dataSource, sqlQueryMapProvider, headers, temporaryBaseDirectory, sheetName);
		
		String fileName = exportor.export();
		return fileName;
	}

}
